#include "shell.h"
#include<string.h>
#include<stdio.h>
#include"user_print.h"



#if 0
extern unsigned int __board_start;
extern unsigned int __board_end;
extern unsigned int __board_first_start;
extern unsigned int __board_first_end;
#endif

#pragma diag_suppress 191
#pragma diag_suppress 1296
#pragma diag_suppress 253
extern unsigned int __fsymtab_start;
extern unsigned int __fsymtab_end;
extern unsigned int __board_start;
extern unsigned int __board_end;
const struct finsh_syscall *finsh_start=( struct finsh_syscall *)(&__fsymtab_start);
const struct finsh_syscall *finsh_end=( struct finsh_syscall *)(&__fsymtab_end);

const unsigned int mmp=0xff;
const unsigned int *mmp1=(const unsigned int *)mmp;
#if 0
const _board_init* board_start=(const _board_init*)(&__board_start);
const _board_init* board_end=(const _board_init*)(&__board_end);
const _board_init* board_first_start=(const _board_init*)(&__board_first_start);
const _board_init* board_first_end=(const _board_init*)(&__board_first_end);
#endif
unsigned int get_data(char*s)
{
    uint32_t i,temp=0;
    for(i=0;(s[i]!=0)&&(s[i]!='"')&&(s[i]!='\r')&&(s[i]!=' ');i++)
    {
        temp*=10;
        temp = temp + (s[i] - '0');
    }
    return temp;
}
unsigned int get_hex(char*s)
{
    uint32_t i,temp=0,data;
    for(i=0;(s[i]!=0)&&(s[i]!='"')&&(s[i]!='\r');i++)
    {
        if(s[i] == '0')
        {
            if(s[i+1] == 'x')
            {
                i += 2;
            }
        }
        if((s[i]>=48)&&(s[i]<=57))
        {
            data = s[i] - 48;
            temp <<= 4;
            temp |= data;
        }
        else if((s[i]>=65)&&(s[i]<=70))
        {
            data = s[i] - 65 + 0xa;
            temp <<= 4;
            temp |= data;
        }
        else if((s[i]>=97)&&(s[i]<=102))
        {
            data = s[i] - 97 + 0xa;
            temp <<= 4;
            temp |= data;
        }
        else {
            user_printf("get_hex error\r\n");
            return temp;
        }
    }
    return temp;
}
char str1[20],str2[30],str3[20];
unsigned int argc=0;
char *argv[20]=
{
  (void*)0,str1,str2,str3
};
static unsigned int parameter_get( char*str, char*shell_cmd)
{
  unsigned int i,j ;
  char match;
  unsigned int match_flag =0;
  unsigned int retval =0;
  argv[0]=str;
  for ( i=0;str[i]!=0;i++)
  {
    if(str[i]=='\r')
    {
      str[i]=0;
      strcpy(shell_cmd,str);
      shell_cmd[i]=0;
      return 1;
    }
    if(str[i]==' ')
    {
      str[i]=0;
      strcpy(shell_cmd,str);
      shell_cmd[i]=0;
      retval=1;
      break;
    }
  }
  j=1;
  match=' ';
  match_flag=0;
  for (i=i+1;str[i]!=0;i++)
  {
    if(str[i]=='\r')
    {
      str[i]=0;
      break;
    }
    if(match_flag==1)
    {
      if(str[i]==match)
      {
        str[i]=0;
        match_flag=0;
        if(match=='"')
        {
          i++;
          str[i]=0;
        }
      }
    }
    else
    {
      if(str[i]==' ')
      {
        break;
      }
      if(str[i]=='"')
      {
        str[i]=0;
        i+=1;
        argv[j]=str+i;
        j++;
        match_flag=1;
        match='"';
        if((str[i+1]!=' ')||(str[i+1]!='\r'))
        {
          retval++;
        }
      }
      else
      {
        argv[j]=str+i;
        j++;
        match_flag=1;
        match=' ';
        if((str[i+1]!=' ')||(str[i+1]!='\r'))
        {
          retval++;
        }
      }
    }
  }
  return retval;
}
void str_std(char*std)
{
  char*p;
  unsigned int j=0,i=0;
  p=std;
  while(p[j]!=0)
  {
    loop:
    if((i==0)&&(p[j]==127))
    {
      j++;
      goto loop;
    }
    if(p[j]!=127)
      p[i]=p[j];
    if(p[j]==127)
    {
      j++;
      i-=1;
      p[i]=p[j];
      goto loop;
    }
    i++;
    j++;
  }
  p[i-1]=0;
}
void fsys(char cmd[])
{
  struct finsh_syscall *p;
  p=(struct finsh_syscall *)finsh_start;
  while(p<(struct finsh_syscall *)finsh_end)
  {
    if(strcmp(cmd,p->name)==0)
    {
      break;
    }
    p++;
  }
  if(p>=(struct finsh_syscall *)finsh_end)
  {
    user_printf("\r\nerror command\r\n");
  }
  else
  {
     p->func(argc,argv);
  }
}


#if 0
void board_device_init()
{
  _board_init *p;
#ifdef DeBug_board_init
  user_printf("!!!!!!board_init!!!!!\r\n\r\n");
#endif
  p=(_board_init*)board_start;
  for(;p<(_board_init*)board_end;p++)
  {
    p->__board_init();
  }
}
void board_device_first_init()
{
  _board_init *p;
#ifdef DeBug_board_first_init
  user_printf("!!!!!!board_first_init!!!!!\r\n\r\n");
#endif
  p=(_board_init*)board_first_start;
  for(;p<(_board_init*)board_first_end;p++)
  {
    p->__board_init();
  }
}
#endif
void display_hex(unsigned char *data,unsigned int len)
{
    unsigned int i;
    for(i = 0; i < len; i++)
    {
        if((i+1)%10 == 0)
        {
            user_printf("\r\n");
        }
        user_printf("0x%02X ",data[i]);
    }
    user_printf("\r\n");
}
void cmd_run(char*cmd)
{
  char shell_cmd[20],shell[20];
  argc=parameter_get(cmd,shell_cmd);

  argv[0]=shell_cmd;
  
  strcpy(shell,"__cmd_");
    // user_printf("%s %s",argv[0],shell);
  strcat(shell,shell_cmd);
  fsys(shell);
}